/*
 * PadPanel.java
 *
 * Created on October 18, 2007, 7:21 PM
 */

package ardrumo;

import javax.swing.ComboBoxModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author  mschaff
 */
public class PadPanel extends javax.swing.JPanel {
	private static int MAXGAINVALUE = 1024;
	private static long BLINKDELAY = 100;
	TriggerPad tp;
	
	public PadPanel(final TriggerPad tp) {
		this.tp = tp;
		initComponents();
		
		SpinnerNumberModel chnls = new SpinnerNumberModel(1,1,16,1);
		spnChannel.setModel(chnls);
		spnChannel.setValue(tp.getChannel());
		spnChannel.setFont(new java.awt.Font("Lucida Grande", 0, 10));
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
        private void initComponents() {
                pnlAdjst = new javax.swing.JPanel();
                lblThresh = new javax.swing.JLabel();
                lblGain = new javax.swing.JLabel();
                cboPadSnd = new javax.swing.JComboBox();
                chkEnabled = new javax.swing.JCheckBox();
                lblLED = new javax.swing.JLabel();
                dknbThresh = new DKnob();
                dknbGain = new DKnob();
                jButton1 = new javax.swing.JButton();
                spnChannel = new javax.swing.JSpinner();
                lblChannel = new javax.swing.JLabel();

                pnlAdjst.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(153, 153, 153), 1, true));
                pnlAdjst.setMaximumSize(new java.awt.Dimension(500, 500));
                pnlAdjst.setMinimumSize(new java.awt.Dimension(500, 500));
                pnlAdjst.setPreferredSize(new java.awt.Dimension(500, 500));
                lblThresh.setFont(new java.awt.Font("Lucida Grande", 0, 10));
                lblThresh.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
                lblThresh.setText("Threshold");

                lblGain.setFont(new java.awt.Font("Lucida Grande", 0, 10));
                lblGain.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
                lblGain.setText("Gain");

                cboPadSnd.setFont(new java.awt.Font("Lucida Grande", 0, 10));
                cboPadSnd.setMaximumRowCount(35);
                cboPadSnd.addItemListener(new java.awt.event.ItemListener() {
                        public void itemStateChanged(java.awt.event.ItemEvent evt) {
                                changeNote(evt);
                        }
                });

                chkEnabled.setFont(new java.awt.Font("Lucida Grande", 0, 10));
                chkEnabled.setSelected(true);
                chkEnabled.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
                chkEnabled.setMargin(new java.awt.Insets(0, 0, 0, 0));
                chkEnabled.addItemListener(new java.awt.event.ItemListener() {
                        public void itemStateChanged(java.awt.event.ItemEvent evt) {
                                changeEnabled(evt);
                        }
                });

                lblLED.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ardrumo/resources/led_off.gif")));

                dknbThresh.setMaxValue("+5");
                dknbThresh.setMinValue("-5");
                dknbThresh.setShowMaxVal(true);
                dknbThresh.setValue(0.5F);
                org.jdesktop.layout.GroupLayout dknbThreshLayout = new org.jdesktop.layout.GroupLayout(dknbThresh);
                dknbThresh.setLayout(dknbThreshLayout);
                dknbThreshLayout.setHorizontalGroup(
                        dknbThreshLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(0, 74, Short.MAX_VALUE)
                );
                dknbThreshLayout.setVerticalGroup(
                        dknbThreshLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(0, 74, Short.MAX_VALUE)
                );

                dknbGain.setMaxValue("+5");
                dknbGain.setMinValue("-5");
                dknbGain.setShowMaxVal(true);
                dknbGain.setValue(0.5F);
                dknbGain.addChangeListener(new javax.swing.event.ChangeListener() {
                        public void stateChanged(javax.swing.event.ChangeEvent evt) {
                                changeGain(evt);
                        }
                });

                org.jdesktop.layout.GroupLayout dknbGainLayout = new org.jdesktop.layout.GroupLayout(dknbGain);
                dknbGain.setLayout(dknbGainLayout);
                dknbGainLayout.setHorizontalGroup(
                        dknbGainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(0, 74, Short.MAX_VALUE)
                );
                dknbGainLayout.setVerticalGroup(
                        dknbGainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(0, 74, Short.MAX_VALUE)
                );

                jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                        public void mousePressed(java.awt.event.MouseEvent evt) {
                                jButton1MousePressed(evt);
                        }
                });

                spnChannel.setFont(new java.awt.Font("Lucida Grande", 0, 10));

                lblChannel.setFont(new java.awt.Font("Lucida Grande", 0, 10));
                lblChannel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
                lblChannel.setText("Channel");

                org.jdesktop.layout.GroupLayout pnlAdjstLayout = new org.jdesktop.layout.GroupLayout(pnlAdjst);
                pnlAdjst.setLayout(pnlAdjstLayout);
                pnlAdjstLayout.setHorizontalGroup(
                        pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(pnlAdjstLayout.createSequentialGroup()
                                .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                .add(pnlAdjstLayout.createSequentialGroup()
                                                        .add(34, 34, 34)
                                                        .add(lblLED)
                                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                                                .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlAdjstLayout.createSequentialGroup()
                                                        .addContainerGap()
                                                        .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                                                .add(spnChannel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                .add(lblChannel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
                                                        .add(22, 22, 22)))
                                        .add(pnlAdjstLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                                .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(pnlAdjstLayout.createSequentialGroup()
                                                .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                                        .add(dknbThresh, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                                                        .add(lblThresh, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                                        .add(pnlAdjstLayout.createSequentialGroup()
                                                                .add(12, 12, 12)
                                                                .add(dknbGain, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 74, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlAdjstLayout.createSequentialGroup()
                                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                .add(lblGain, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                .add(20, 20, 20))))
                                        .add(pnlAdjstLayout.createSequentialGroup()
                                                .add(chkEnabled)
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 12, Short.MAX_VALUE)
                                                .add(cboPadSnd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 141, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                                .addContainerGap())
                );
                pnlAdjstLayout.setVerticalGroup(
                        pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(pnlAdjstLayout.createSequentialGroup()
                                .addContainerGap()
                                .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                        .add(pnlAdjstLayout.createSequentialGroup()
                                                .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                        .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                                                .add(chkEnabled)
                                                                .add(cboPadSnd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                        .add(pnlAdjstLayout.createSequentialGroup()
                                                                .add(lblLED)
                                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                                .add(21, 21, 21)
                                                .add(spnChannel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                        .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, dknbGain, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, dknbThresh, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 74, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                        .add(pnlAdjstLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                                .add(lblGain, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .add(lblThresh, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .add(lblChannel))
                                .addContainerGap())
                );

                org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
                this.setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(pnlAdjst, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 263, Short.MAX_VALUE)
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(pnlAdjst, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 155, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                );
        }// </editor-fold>//GEN-END:initComponents

	private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
		setLEDBlink();
		tp.playNote(100);
	}//GEN-LAST:event_jButton1MousePressed

	private void changeGain(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_changeGain
		
		float f = dknbGain.getValue();
		
		if(f > 0) {
			tp.setGain( (int)(f * MAXGAINVALUE ));
		} else {
			tp.setGain(0);
		}
	}//GEN-LAST:event_changeGain

	private void changeNote(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_changeNote
		tp.setPadsound(cboPadSnd.getSelectedItem().toString());
	}//GEN-LAST:event_changeNote

	private void changeEnabled(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_changeEnabled
		tp.setEnabled(chkEnabled.isSelected());
	}//GEN-LAST:event_changeEnabled
	
	public void setSoundCombobox(String[] s) {
		cboPadSnd.removeAllItems();
		
		for(int i=0; i<s.length; i++) {
			cboPadSnd.addItem(s[i]);
		}
	}
	
	public void setSelectedSound(String s) {
		cboPadSnd.setSelectedItem(s);
	}
	
	public int getGain() {
		float f = dknbGain.getValue();
		
		if(f > 0) {
			return (int)(f * MAXGAINVALUE );
		} else {
			return 0;
		}
	}
	
	public void setLEDBlink() {
		Runnable rnbl = new LEDBlinkThread(BLINKDELAY);
		Thread thrd = new Thread(rnbl);
		thrd.start();
	}
	
	public void setLEDOn() {
		lblLED.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ardrumo/resources/led_on.gif")));
		
	}
	
	public void setLEDOff() {
		lblLED.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ardrumo/resources/led_off.gif")));
	}
	
	class LEDBlinkThread implements Runnable {
		long delay = 0;
		
		public LEDBlinkThread(long d) {
			this.delay = d;
		}
		
		public void run() {
			setLEDOn();
			try {
				Thread.sleep(delay);
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
			setLEDOff();
		}
	}
	
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JComboBox cboPadSnd;
        private javax.swing.JCheckBox chkEnabled;
        private DKnob dknbGain;
        private DKnob dknbThresh;
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel lblChannel;
        private javax.swing.JLabel lblGain;
        private javax.swing.JLabel lblLED;
        private javax.swing.JLabel lblThresh;
        private javax.swing.JPanel pnlAdjst;
        private javax.swing.JSpinner spnChannel;
        // End of variables declaration//GEN-END:variables
	
}
